package com.weishenai.customerassistant.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weishenai.customerassistant.entity.Electrical;
import com.weishenai.customerassistant.mapper.ElectricalMapper;
import com.weishenai.customerassistant.service.ElectricalService;
import com.weishenai.customerassistant.utils.DateUtils;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Set;

@Service
public class ElectricalServiceImpl extends ServiceImpl<ElectricalMapper, Electrical> implements ElectricalService {

    @Autowired
    private ElectricalMapper electricalMapper;

    @Override
    public Electrical getElectricalByWorkId(Integer workerId) {
        QueryWrapper<Electrical> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("worker_id", workerId);
        return electricalMapper.selectOne(queryWrapper);
    }

    @Override
    public Integer insertElectrical(Electrical area) {
        area.setCreatedTime(DateUtils.getTime());
        return electricalMapper.insert(area);
    }

    @Override
    public Integer delElectrical(Integer id) {
        return electricalMapper.deleteById(id);
    }

    @Override
    public Integer updateElectrical(Electrical electrical) {
        return electricalMapper.updateById(electrical);
    }

    @Override
    public IPage<Electrical> getElectrical(Integer pageNumber, Integer pageSize, String keyword) {
//        QueryWrapper<Electrical> queryWrapper = new QueryWrapper<>();
//        if (!StringUtil.isNullOrEmpty(keyword)) {
//            queryWrapper.like("electrical_transformer_name", keyword);
//        }
//        queryWrapper.orderByDesc("created_time");
//        IPage<Electrical> page = new Page<>(pageNumber, pageSize);
//        return electricalMapper.selectPage(page, queryWrapper);
        return electricalMapper.selectPage(new Page<>(pageNumber,pageSize),
                Wrappers.lambdaQuery(Electrical.class)
                        .like(Objects.nonNull(keyword),Electrical::getElectricalTransformerName,keyword));
    }

    @Override
    public void Excel2DB(Electrical electrical) {
        int result = electricalMapper.updateById(electrical);
        if (result == 0) {
            electrical.setCreatedTime(DateUtils.getTime());
            electricalMapper.insert(electrical);
        }
    }

    @Override
    public List<Electrical> selectByName(String name) {
        return electricalMapper.selectByName(name);
    }

    @Override
    public Set<String> selectNames() {
        return electricalMapper.selectName();
    }

    @Override
    public List<Electrical> selectAll() {
        return electricalMapper.selectList(null);
    }

    @Override
    public Electrical selectById(Integer id) {
        QueryWrapper<Electrical> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        return electricalMapper.selectOne(queryWrapper);
    }
}
